<?php

namespace App\Http\Controllers\Operation;

use App\Http\Controllers\Controller;
use App\Http\Requests\Operation\ContentTemplateTypeRequest;
use App\Models\Operation\ContentTemplateType;
use App\Repositories\Operation\ContentTemplateTypeRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Response;

class ContentTemplateTypeController extends Controller
{
    use ModelForm;

    private $contentTemplateTypeRepository;

    public function __construct(ContentTemplateTypeRepository $contentTemplateTypeRepo)
    {
        $this->contentTemplateTypeRepository = $contentTemplateTypeRepo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('operation.content_template_types.index_header'));
            $content->description(__('operation.content_template_types.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('operation.content_template_types.create_header'));
            $content->description(__('operation.content_template_types.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ContentTemplateTypeRequest $request)
    {
        $input = $request->all();

        $contentTemplateType = $this->contentTemplateTypeRepository->create($input);

        Flash::success(__('operation.content_template_types.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('operation.content_template_types.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $contentTemplateType = $this->contentTemplateTypeRepository->findWithoutFail($id);

        if (empty($contentTemplateType)) {
            Flash::error(__('operation.content_template_types.not_found'));

            return redirect(route('operation.content_template_types.index'));
        }

        return view('operation.content_template_types.show')->with('contentTemplateType', $contentTemplateType);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('operation.content_template_types.edit_header'));
            $content->description(__('operation.content_template_types.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ContentTemplateTypeRequest $request, $id)
    {
        $contentTemplateType = $this->contentTemplateTypeRepository->findWithoutFail($id);

        if (empty($contentTemplateType)) {
            Flash::error(__('operation.content_template_types.not_found'));

            return redirect(route('operation.content_template_types.index'));
        }

        $input = $request->all();

        $contentTemplateType = $this->contentTemplateTypeRepository->update($input, $id);

        Flash::success(__('operation.content_template_types.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('operation.content_template_types.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->contentTemplateTypeRepository->batchDelete('id', $id)) {
                return response()->json(['message' => __('operation.content_template_types.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $contentTemplateType = $this->contentTemplateTypeRepository->findWithoutFail($id);

        if (empty($contentTemplateType)) {
            Flash::error(__('operation.content_template_types.not_found'));

            return redirect(route('operation.content_template_types.index'));
        }

        if ($flag = $this->contentTemplateTypeRepository->delete($id)) {
            return response()->json(['message' => __('operation.content_template_types.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    protected function form()
    {
        return Admin::form(ContentTemplateType::class, function (Form $form) {
            // ID
            $form->display('id', 'ID');
            // 内容模板类型名称
            $form->text('name', '内容模板类型名称')->help('必填');
            // 内容模板类型别名
            $form->text('alias', '内容模板类型别名')->help('必填');
            // 描述
            $form->textarea('description', '描述');
            // 排序
            $form->number('listorder', '排序')->default(0);

            $form->display('created_at', __('base.created_at'));
            $form->display('updated_at', __('base.updated_at'));
        });
    }

    protected function grid()
    {

        return Admin::grid(ContentTemplateType::class, function (Grid $grid) {
            $grid->model()->orderBy('listorder', 'asc');

            // //添加按钮
            // if (!\Gate::check('operation.content_template.create')) {
            //     $grid->disableCreation();
            // }

            // // 编辑和删除按钮
            // $grid->actions(function ($actions) {
            //     //编辑按钮
            //     if (!\Gate::check('operation.content_template.edit')) {
            //         $actions->disableEdit();
            //     }
            //     //删除按钮
            //     if (!\Gate::check('operation.content_template.destroy')) {
            //         $actions->disableDelete();
            //     }
            // });

            // //导出按钮
            // if (!\Gate::check('operation.content_template.export')) {
            //     $grid->disableExport();
            // }

            // //批量操作
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         //批量删除按钮
            //         if (!\Gate::check('operation.content_template.batch_destroy')) {
            //             $batch->disableDelete();
            //         }
            //     });
            // });

            $grid->column('name', '内容模板类型名称')->sortable();
            // $grid->column('video')->openVideo(function () {

            //     return ['30.254248196081456', '120.16402333974838'];

            // }, 'video');
            $grid->column('alias', '模板类型别名')->sortable();
            $grid->column('listorder', '排序')->sortable();

            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();
                // 名称
                $filter->like('name', '内容模板类型名称');
                // 模板别名
                $filter->like('alias', '模板类型别名');
            });
        });
    }
}
